namespace QMVnSoft.Controls
{
    using QMVnSoft;
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Drawing;
    using System.Windows.Forms;

    public class GridControl : DataGridView
    {
        private bool ib_CheckAutoSize;
        private bool ib_CheckColor;
        private bool ib_FirstTimeChanged = true;
        private bool ib_GridSizeChanged;
        private bool ib_NoChangeEvent;
        public Hashtable idDeletes = new Hashtable();

        private void GridControl_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            DataGridView view = (DataGridView) sender;
            view.EditMode = DataGridViewEditMode.EditOnKeystrokeOrF2;
            if (base.FindForm() is FormControl)
            {
                (base.FindForm() as FormControl).ActiveGridBrowser = this;
            }
        }

        private void GridControl_MouseClick(object sender, MouseEventArgs e)
        {
            DataGridView view = (DataGridView) sender;
            if (view.HitTest(e.X, e.Y).Type == DataGridViewHitTestType.RowHeader)
            {
                view.EditMode = DataGridViewEditMode.EditOnF2;
                view.EndEdit();
            }
        }

        protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e)
        {
            if (base.ReadOnly)
            {
                try
                {
                    if (this.ib_CheckColor)
                    {
                        if (((base.FindForm().ActiveControl as GridControl).Equals(this) && (e.RowIndex == base.CurrentCell.RowIndex)) && (e.ColumnIndex == base.CurrentCell.ColumnIndex))
                        {
                            e.CellStyle.SelectionBackColor = Color.FromArgb(51, 153, 255);
                        }
                    }
                    else if ((e.RowIndex == base.CurrentCell.RowIndex) && (e.ColumnIndex == base.CurrentCell.ColumnIndex))
                    {
                        e.CellStyle.SelectionBackColor = Color.FromArgb(51, 153, 255);
                    }
                }
                catch
                {
                }
            }
            base.OnCellPainting(e);
        }

        protected override void OnColumnWidthChanged(DataGridViewColumnEventArgs e)
        {
            base.OnColumnWidthChanged(e);
            if (!this.ib_NoChangeEvent)
            {
                this.SetColumnWidths();
            }
        }

        protected override void OnCreateControl()
        {
            this.Font = AppInstance.SystemFont;
            base.CellEnter += new DataGridViewCellEventHandler(this.GridControl_CellEnter);
            base.MouseClick += new MouseEventHandler(this.GridControl_MouseClick);
            base.VerticalScrollBar.VisibleChanged += new EventHandler(this.VerticalScrollBar_VisibleChanged);
            this.ib_GridSizeChanged = true;
            this.SetColumnWidths();
            base.OnCreateControl();
        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            if (!this.ib_FirstTimeChanged)
            {
                this.ib_GridSizeChanged = true;
                this.SetColumnWidths();
            }
            else
            {
                this.ib_FirstTimeChanged = false;
            }
        }

        public void SetColumnWidths()
        {
            if (this.CheckAutoSize)
            {
                this.ib_NoChangeEvent = true;
                int num = 0;
                int num2 = ((base.Width - base.RowHeadersWidth) - this.DataGridViewScrollWidth) - 2;
                DataGridViewColumn column = null;
                foreach (DataGridViewColumn column2 in base.Columns)
                {
                    if (!column2.Visible)
                    {
                        continue;
                    }
                    if (column2.Resizable == DataGridViewTriState.True)
                    {
                        if (column == null)
                        {
                            column = column2;
                        }
                        if (column2.Width > column.Width)
                        {
                            column = column2;
                        }
                        num += column2.Width;
                        continue;
                    }
                    num2 -= column2.Width;
                }
                if (this.ib_GridSizeChanged || (num < num2))
                {
                    int num3 = num2 - num;
                    int num4 = 0;
                    foreach (DataGridViewColumn column3 in base.Columns)
                    {
                        if (!column3.Visible || (column3.Resizable != DataGridViewTriState.True))
                        {
                            continue;
                        }
                        if ((column3.Width + (column3.Width * (num3 / num))) > 0)
                        {
                            column3.Width += column3.Width * (num3 / num);
                        }
                        else
                        {
                            column3.Width = 0;
                        }
                        num4 += column3.Width;
                    }
                    if (num4 != num2)
                    {
                        if ((column.Width + (num2 - num4)) > 0)
                        {
                            column.Width += num2 - num4;
                        }
                        else
                        {
                            column.Width = 0;
                        }
                    }
                }
                this.ib_GridSizeChanged = false;
                this.ib_NoChangeEvent = false;
            }
        }

        private void VerticalScrollBar_VisibleChanged(object sender, EventArgs e)
        {
            this.ib_GridSizeChanged = true;
            this.SetColumnWidths();
        }

        [DefaultValue(false), Description("Get or set auto size.")]
        public bool CheckAutoSize
        {
            get
            {
                return this.ib_CheckAutoSize;
            }
            set
            {
                this.ib_CheckAutoSize = value;
            }
        }

        public bool CheckColor
        {
            get
            {
                return this.ib_CheckColor;
            }
            set
            {
                this.ib_CheckColor = value;
            }
        }

        [Browsable(false)]
        public int DataGridViewScrollWidth
        {
            get
            {
                if (base.VerticalScrollBar.Visible)
                {
                    return base.VerticalScrollBar.Width;
                }
                return 0;
            }
        }
    }
}

